﻿

$(document).ready(function () {
    scheduleSetIframeHeight();
});

////设置Iframe高度
//var timeout;
//function scheduleSetIframeHeight() {
//    $('iframe').height($('#navTabPanel').height());
//    window.clearTimeout(timeout);
//    timeout = window.setTimeout(
//                function () { scheduleSetIframeHeight(); },
//                1500
//     );
//}
//设置Iframe高度
var timeout;
function scheduleSetIframeHeight() {
    $('iframe').height($('#navTabPanel').height());

    //目标高度
    var h1 = -1;
    var h2 = -1;
    var h3 = -1;

    var targetHeight = $('#navTabPanel').height();
    $('iframe').each(function () {
        var hasContent = $(this).contents().find(".ui-content").length > 0;
        if (!hasContent) { return true; }
        var hasMenu = ($(this).contents().find(".ui-menu").length > 0);
        var hasStatus = $(this).contents().find(".ui-status").length > 0;
        if (hasMenu && hasStatus) {
            if (h1 != $(this).contents().find(".ui-content").height()) {
                $(this).contents().find(".ui-content").height(targetHeight - 62);
                h1 = targetHeight - 62;
            }
        } else if (hasMenu || hasStatus) {
            if (h2 != $(this).contents().find(".ui-content").height()) {
                $(this).contents().find(".ui-content").height(targetHeight - 31);
                h2 = targetHeight - 31;
            }
        } else {
            if (h3 != $(this).contents().find(".ui-content").height()) {
                $(this).contents().find(".ui-content").height(targetHeight);
                h3 = targetHeight;
            }
        }
    });

    window.clearTimeout(timeout);
    timeout = window.setTimeout(
                function () { scheduleSetIframeHeight(); },
                1500
     );
}


//打开模块主页面
var openModuleHome = function (tabid, url, title) {
    if (!tabid) { return; }
    navTab.openTab(tabid, url, { title: title, fresh: false, external: true, data: {} });
}

//更改密码
var doChangePassword = function () {
    var oldPassword = $('#txtOldPassword').val();
    var newPassword = $('#cp_newPassword').val();
    var cp_rnewPassword = $('#cp_rnewPassword').val();

    if (!oldPassword) {
        alertMsg.correct('旧密码不能为空！');
        return;
    }

    if (!oldPassword) {
        alertMsg.correct('新密码不能为空！');
        return;
    }

    if (!oldPassword) {
        alertMsg.correct('重复密码不能为空！');
        return;
    }

    if (newPassword != cp_rnewPassword) {
        alertMsg.correct('与重复密码不一致！');
        return;
    }

    SGDemo.Web.Main.ChangePassword(oldPassword, newPassword, function (result) {
        if (result.value) {
            alertMsg.correct('修改密码成功！');
            $.pdialog.closeCurrent();
        } else {
            alertMsg.error('修改密码失败！请仔细检查您的输入是否有误');
        }
    });
}

//重新登录
var doRelogin = function () {
    if (confirm('确认重新登录?')) {
        SGDemo.Web.Main.Relogin(function (result) {
            location.href = 'login.aspx';
        });
    }
}

//退出
var doLoginout = function () {
    SGDemo.Web.Main.Relogin();
}

//获取个人资料
var doGetProfile = function () {
    SGDemo.Web.Main.GetProfile(function (result) {
        var ret = eval("(" + result.value + ")");
        $('#txtUserName').val(ret.UserName);
        $('#txtRealName').val(ret.RealName);
        $('#txtMobilePhone').val(ret.MobilePhone);
        $('#txtEmail').val(ret.Email);

    });
}

//保存个人资料
var doSaveProfile = function () {
    var realName = $('#txtRealName').val();
    var mobilePhone = $('#txtMobilePhone').val();
    var email = $('#txtEmail').val();
                

    $.pdialog.closeCurrent();
    SGDemo.Web.Main.SaveProfile(realName, mobilePhone, email, function (result) {
        if (result.value) {
            alertMsg.correct('修改资料成功！');
        } else {
            alertMsg.error('修改资料失败！请仔细检查您的输入是否有误');
        }
    });
}

//获取密码问题
var doGetOldPasswordQuestion = function () {
    SGDemo.Web.Main.GetOldPasswordQuestion(function (result) {
        $('#txtOldPasswordQuestion').val(result.value);
    });
}

var doSelectChange = function () {
    var val = $('#selPasswordQuestion').val();
    if (val == '0') {
        $('#divPasswordQuestion').show();
        $('#selPasswordQuestion').removeClass('required');
        $('#txtPasswordQuestion').addClass('required');
    } else {
        $('#divPasswordQuestion').hide();
        $('#selPasswordQuestion').addClass('required');
        $('#txtPasswordQuestion').removeClass('required');
    }
}

//修改密保
var doSavePasswordSecurity = function () {


    var oldPassword = $('#txtOldPasswordAnswer').val();
    if (!oldPassword) {
        alert('请输入旧的密保问题');
        $('#txtOldPasswordAnswer').focus();
        return;
    }

    var newPasswordQuestion = $('#selPasswordQuestion').val();

    if (newPasswordQuestion == '0') {
        newPasswordQuestion = $('#txtPasswordQuestion').val();
    }

    if (newPasswordQuestion == '') {
        alert('请选择或者输入新的密保问题');
        $('#selPasswordQuestion').focus();
        return;
    }

    var newPasswordAnswer = $('#txtPasswordAnswer').val();
    if (!oldPassword) {
        alert('请输入新的的密保问题');
        $('#txtPasswordAnswer').focus();
        return;
    }

    $.pdialog.closeCurrent();
    SGDemo.Web.Main.SavePasswordSecurity(oldPassword, newPasswordQuestion, newPasswordAnswer, function (result) {
        if (result.value) {
            alertMsg.correct('修改密保成功！');
        } else {
            alertMsg.error('回答密保问题错误！');
        }
    });

}


